// Vimeo.js for PSPTube
// Made By JK108

function Vimeo_CheckURL( url, option )
{
	if (url.match("http://www.vimeo.com") == "http://www.vimeo.com"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a Vimeo URL
*/
function Vimeo_GetURL( url, option )
{
	var contents = GetContents("http://keepvid.com/?url=" + url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/\<\/script\>\<br \/\>\<br \/\>\<a href\=\"(.*?)\"/);
	var video_url = video_url[1];
	return video_url;
}

function Vimeo_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 12;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://www.vimeo.com/videos/search:" + keyword.replace(/ /g, "+") + "/page:1/sort:relevent/format:detail" );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://www.vimeo.com/videos/search:" + keyword.replace(/ /g, "+") + "/page:" + num + "/sort:relevent/format:detail" );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("<div id=\"none_msg\">Sorry, nothing found.</div>") == "<div id=\"none_msg\">Sorry, nothing found.</div>"){ result.total = 0; return result; }
	var total = contents.match(/We found (.*?) videos./);
	var total = total[1].replace(/,/g, "");
	result.total = total;


	while(1){
	var pos = contents.indexOf("<a class=\"thumbnail\"");

	if(pos != -1){
		contents = contents.substring(pos + 20, contents.length);
		//Get URL
		var url = contents.match(/href\=\"(.*?)\"/);
		var url = "http://www.vimeo.com" + url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\<img src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		var title = contents.match(/\<div class\=\"title\"\>(.*?)\<\/div\>/);
		var title = ImproveText(title[1]);
		//Description
		pos = contents.indexOf("<div class=\"description\">");
		var description = contents.substring(pos + 25, contents.length);
		pos = description.indexOf("</div>");
		description = splitTo2Lines(ImproveText(description.substring(0, pos)), 60);
		//Views
		var views = contents.match(/\<span class\=\"plays\"\>(.*?)\<\/span\>/);
		var views = parseInt(views[1].replace(/,/g, ""));
		//Comments
		var comments = contents.match(/\<span class\=\"comments\"\>(.*?)\<\/span\>/);
		var comments = parseInt(comments[1].replace(/,/g, ""));
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.Description = description;
		info.ViewCount = views;
		info.CommentCount = comments;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var Vimeo = new Object();
Vimeo.Name          = "Vimeo";		/* Service name */
Vimeo.Description   = "Vimeo";		/* Service description */
Vimeo.SearchDesc    = "Vimeo";		/* Service search screen description */
Vimeo.SearchOSKMode = 1;		/* OSK mode 1: English only  2: Japanese */
Vimeo.CheckURL      = Vimeo_CheckURL;	/* CheckURL function */
Vimeo.GetURL        = Vimeo_GetURL;	/* GetURL function */
Vimeo.Search        = Vimeo_Search;	/* Search function */

// add Vimeo to site list
SiteList.push( Vimeo );
